/*
 * Movie Renamer
 * Copyright (C) 2012-2013 Nicolas Magré
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.free.movierenamer.ui.swing.panel.generator.info;

import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import fr.free.movierenamer.info.MediaInfo;
import fr.free.movierenamer.ui.utils.ImageUtils;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Class VideoPanel
 *
 * @author Nicolas Magré
 */
public abstract class VideoPanel<T extends MediaInfo> extends MediaPanel<T> {

  private final int nbStar = 5;
  private final WebPanel starPanel;
  private final List<WebLabel> stars;
  private T info;

  /**
   * Creates new form VideoPanel
   *
   * @param panels
   */
  protected VideoPanel(InfoPanel<T>... panels) {
    super();
    initComponents();

    starPanel = new WebPanel();
    starPanel.setMargin(0);
    starPanel.setLayout(new FlowLayout());
    stars = new ArrayList<WebLabel>();
    for (int i = 0; i < nbStar; i++) {
      WebLabel label = new WebLabel();
      label.setMargin(new Insets(-3, 0, 0, 0));
      stars.add(label);
      starPanel.add(stars.get(i));
    }

    mainTb.addToEnd(starPanel);
    createPanel(infoPanels, Arrays.asList(panels));

    clearStars();
  }

  @Override
  protected void setLoading(boolean loading) {
    super.setLoading(loading);
    mediaTitleLbl.setIcon(loading ? ImageUtils.LOAD_16 : null);
  }

  @Override
  public T getInfo() {
    return info;
  }

  @Override
  public void addInfo(T info) {
    this.info = info;
    for (InfoPanel<T> panel : panels) {
      panel.setInfo(info);
    }

    mediaTitleLbl.setText(getTitle(info));
    setRate(getRate(info));
  }

  protected abstract String getTitle(T info);

  protected abstract Double getRate(T info);

  private void setRate(Double rate) {
    Double value = rate;
    if (value == null || value < 0.00) {
      return;
    }

    if (value > 5) {
      value /= (10 / nbStar);
    }

    int n = value.intValue();
    for (int i = 0; i < n; i++) {
      stars.get(i).setIcon(ImageUtils.STAR_16);
    }

    if ((rate - rate.intValue()) >= 0.50 && n < nbStar) {
      stars.get(n).setIcon(ImageUtils.STARHALF_16);
    }
  }

  private void clearStars() {
    for (int i = 0; i < nbStar; i++) {
      stars.get(i).setIcon(ImageUtils.STAREMPTY_16);
    }
  }

  @Override
  public final void clear() {
    super.clear();
    info = null;
    clearStars();
    mediaTitleLbl.setText("");
  }

  /**
   * This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    mainTb = new com.alee.laf.toolbar.WebToolBar();
    mediaTitleLbl = new com.alee.laf.label.WebLabel();
    infoPanels = new com.alee.laf.panel.WebPanel();

    setAutoscrolls(true);

    mainTb.setFloatable(false);
    mainTb.setRollover(true);

    mediaTitleLbl.setFont(new java.awt.Font("Ubuntu", 1, 14)); // NOI18N
    mediaTitleLbl.setMargin(new java.awt.Insets(0, 0, 0, 10));
    mainTb.add(mediaTitleLbl);

    infoPanels.setAutoscrolls(true);
    infoPanels.setLayout(new java.awt.GridBagLayout());

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(infoPanels, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(mainTb, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(mainTb, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(infoPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private com.alee.laf.panel.WebPanel infoPanels;
  private com.alee.laf.toolbar.WebToolBar mainTb;
  private com.alee.laf.label.WebLabel mediaTitleLbl;
  // End of variables declaration//GEN-END:variables
}
